{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import numpy as np\n",
    "import cv2\n",
    "from tqdm import tqdm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_data_root = 'D:/Data/AerialImageDataset/test/'\n",
    "test_input_folder = test_data_root + 'predicted_masks_vgg16_bcedice/0/'\n",
    "test_output_folder = test_data_root + 'integrated/'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "180\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|████████████████████████████████████████████████████████████████████████████████| 180/180 [07:13<00:00,  2.41s/it]\n"
     ]
    }
   ],
   "source": [
    "name_set = set([filename.split('_')[0] for filename in os.listdir(test_input_folder)])\n",
    "print(len(name_set))\n",
    "size=256\n",
    "for name in tqdm(name_set):\n",
    "    output_img = np.zeros([5120,5120])\n",
    "    for row in range(20):\n",
    "        for col in range(20):\n",
    "            output_name = name + '.tif'\n",
    "            part_name = '{}_{}_{}.jpg'.format(name,row,col)\n",
    "            part_img = cv2.imread(test_input_folder + part_name, cv2.IMREAD_GRAYSCALE)\n",
    "            # 再做一次二值化， threshold = 0.5\n",
    "            part_img = cv2.threshold(part_img, 127, 255,cv2.THRESH_BINARY)[1]\n",
    "            output_img[row*size:(row+1)*size, col*size:(col+1)*size] = part_img\n",
    "    output_img = output_img[60:5060,60:5060]\n",
    "#     print(output_img.shape)\n",
    "    cv2.imwrite(test_output_folder+output_name, output_img)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
